﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace System.Web.Mvc.Html
{
    /// <summary>
    /// HTML标记扩展
    /// </summary>
    public static class HtmlExtensions
    {

        #region 超链接的扩展（后台权限设计）
        public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, Enum operatorAuthority)
        {
            if ((Convert.ToInt32((System.Web.HttpContext.Current.Session["OperatorAuthority"] ?? "0").ToString()) & Convert.ToInt32(operatorAuthority)) == 0)
                return null;
            else
                return htmlHelper.ActionLink(linkText, actionName, null, new { @class = "button" });
        }
        public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, Enum operatorAuthority)
        {
            if ((Convert.ToInt32((System.Web.HttpContext.Current.Session["OperatorAuthority"] ?? "0").ToString()) & Convert.ToInt32(operatorAuthority)) == 0)
                return null;
            else
                return htmlHelper.ActionLink(linkText, actionName, routeValues, new { @class = "button" });
        }
        public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes, Enum operatorAuthority)
        {
            if ((Convert.ToInt32((System.Web.HttpContext.Current.Session["OperatorAuthority"] ?? "0").ToString()) & Convert.ToInt32(operatorAuthority)) != 0)
                return htmlHelper.ActionLink(linkText, actionName, routeValues, htmlAttributes ?? new { @class = "button" });
            else
                return null;
        }

        public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, Enum operatorAuthority)
        {
            if ((Convert.ToInt32((System.Web.HttpContext.Current.Session["OperatorAuthority"] ?? "0").ToString()) & Convert.ToInt32(operatorAuthority)) == 0)
                return null;
            else
                return htmlHelper.ActionLink(linkText, actionName, controllerName, new { @class = "button" });
        }
        public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, Enum operatorAuthority)
        {
            return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, new { @class = "button" });
        }
        public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes, Enum operatorAuthority)
        {
            if ((Convert.ToInt32((System.Web.HttpContext.Current.Session["OperatorAuthority"] ?? "0").ToString()) & Convert.ToInt32(operatorAuthority)) == 0)
                return null;
            else
            {
                return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, new { @class = "button" });

            }

        }
        #endregion 超链接的扩展（后台权限设计）

        #region Submit按钮扩展
        /// <summary>
        /// 建立一个submit类型的按钮
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="buttonText"></param>
        /// <param name="userOperatorRole"></param>
        /// <returns></returns>
        public static MvcHtmlString CreateButton(this HtmlHelper htmlHelper, string buttonText, Enum operatorAuthority)
        {
            if ((Convert.ToInt32((System.Web.HttpContext.Current.Session["OperatorAuthority"] ?? "0").ToString()) & Convert.ToInt32(operatorAuthority)) == 0)
                return new MvcHtmlString("<input type='submit' value='" + buttonText + "' disabled='disabled'/>");
            else
                return new MvcHtmlString("<input type='submit' value='" + buttonText + "' />");
        }

        #endregion

        #region 自定义的超链接，为权限设计

        /// <summary>
        /// 自定义链接，根据包的状态及用户的权限控制按钮是否被显示，产生A标签的ID属性
        /// 自定义链接，带有权限控制的
        /// 用于调用一个JS方法
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText">链接名称</param>
        /// <param name="js">是否是JS方法</param>
        /// <param name="url">JS方法名或URL</param>
        /// <param name="userOperatorRole">权限 </param>
        /// <returns></returns>
        public static MvcHtmlString CreateActionLink(this HtmlHelper htmlHelper, string id, string className, string linkText, bool js, string url, Enum userOperatorRole)
        {

            if ((Convert.ToInt32(System.Web.HttpContext.Current.Session["UserOperatorRole"]) & Convert.ToInt32(userOperatorRole)) != 0)
            {
                if (js)
                {
                    if (string.IsNullOrEmpty(id))
                        return MvcHtmlString.Create("<a class=\"" + className + " button\" href=\"javascript:void(0)\" onclick=\"" + url + ";return false;\")' title=\"" + linkText + "\">" + linkText + "</a>");
                    else
                        return MvcHtmlString.Create("<a id=\"" + id + "\" name=\"" + id + "\" class=\"" + className + " button\" href=\"javascript:void(0)\" onclick=\"" + url + ";return false;\")' title=\"" + linkText + "\">" + linkText + "</a>");
                }
                else
                {
                    return MvcHtmlString.Create("<a  class=\"" + className + " button\" href=\"" + url + "\"  title=\"" + linkText + "\">" + linkText + "</a>");
                }
            }

            else
                return null;
        }
        /// <summary>
        /// 自定义链接，带有权限控制的
        /// 不产生A标签的ID属性
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="js"></param>
        /// <param name="url"></param>
        /// <param name="userOperatorRole"></param>
        /// <returns></returns>
        public static MvcHtmlString CreateActionLink(this HtmlHelper htmlHelper, string linkText, bool js, string url, Enum userOperatorRole)
        {
            return CreateActionLink(htmlHelper, " ", null, linkText, true, url, userOperatorRole);
        }
        /// <summary>
        /// 自定义链接，带有权限控制的
        /// 进行普通的链接跳转
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="linkText">链接名称</param>
        /// <param name="url">链接地址</param>
        /// <param name="userOperatorRole">权限</param>
        /// <returns></returns>
        public static MvcHtmlString CreateActionLink(this HtmlHelper htmlHelper, string linkText, string url, Enum userOperatorRole)
        {
            return CreateActionLink(htmlHelper, " ", null, linkText, false, url, userOperatorRole);
        }

        #endregion 自定义的超链接，为权限设计
    }
}
